package com.data.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.annotation.ApiLogDetail;
import com.common.base.BaseEnum;
import com.common.exception.BaseException;
import com.common.vo.FileInfoVo;
import com.common.vo.res.FileInfoRes;
import com.data.core.service.FileInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.data.core.dao.FileInfoMapper;
import com.data.core.entry.FileInfo;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * @version: V1.0
 * @author: szw
 * @className: FileInfoServiceImpl
 * @packageName: FileInfoServiceImpl
 * @description: 文件接口实现
 * @data: 2021-1-19
 **/
@Service
public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> implements FileInfoService {

    //文件上传路径
    private final String fileRootPath = "D:\\ys\\files\\";

    @Resource
    private FileInfoMapper fileInfoMapper;


    /**
     * 文件上传接口实现
     *
     * @param fileInfoRes
     * @param files
     * @return
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-文件上传接口实现",operationType=BaseEnum.INSERT)
    public FileInfoVo upload(FileInfoRes fileInfoRes, MultipartFile[] files) {
        String filePath = "";
        try {
            // 多文件上传
            for (MultipartFile file : files) {
                // 上传简单文件名
                String originalFilename = file.getOriginalFilename();
                // 存储路径
                filePath = new StringBuilder(fileRootPath)
                        .append(System.currentTimeMillis())
                        .append(originalFilename)
                        .toString();

                // 保存文件
                file.transferTo(new File(filePath));
                FileInfo fileInfo = FileInfo.builder().
                        fileName(fileInfoRes.getFileName()).fileType(fileInfoRes.getFileType())
                        .created(fileInfoRes.getCreated()).fileUrl(filePath).createdDate(LocalDateTime.now())
                        .enable(fileInfoRes.getEnable()).build();
                int result = fileInfoMapper.insert(fileInfo);
                if (result > 0) {
                    FileInfoVo fileInfoVo = new FileInfoVo();
                    BeanUtils.copyProperties(fileInfo, fileInfoVo);
                    return fileInfoVo;
                }
            }
        } catch (IOException e) {
            throw new BaseException(BaseEnum.FILE_UPLOAD_IS_ERROR, e);
        }
        return null;
    }

    /**
     * 查询附件接口实现
     * @param fileInfoRes
     * @return FileInfoVo
     */
    @Override
    @ApiLogDetail(detail = "核心服务-查询附件接口实现",operationType=BaseEnum.SELECT)
    public FileInfoVo findFile(FileInfoRes fileInfoRes) {
        if (null == fileInfoRes.getId()) {
            throw new BaseException(BaseEnum.FILE_ID_IS_NULL);
        }
        FileInfo fileInfo = fileInfoMapper.selectById(fileInfoRes.getId());
        FileInfoVo fileInfoVo = new FileInfoVo();
        BeanUtils.copyProperties(fileInfo, fileInfoVo);
        return fileInfoVo;
    }

    /**
     * 删除附件接口实现
     *
     * @param id
     * @return int
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-删除附件接口实现",operationType=BaseEnum.DELETE)
    public int delete(Long id) {
        if (null == id) {
            throw new BaseException(BaseEnum.FILE_ID_IS_NULL);
        }
        return fileInfoMapper.deleteById(id);
    }


    /**
     * 查询附件列表接口实现
     *
     * @param fileInfoRes
     * @return IPage<FileInfo>
     */
    @Override
    @ApiLogDetail(detail = "核心服务-查询附件列表接口实现",operationType=BaseEnum.SELECT)
    public IPage<FileInfo> findFileInfoVos(Page<FileInfo> page, FileInfoRes fileInfoRes) {
        if (Objects.isNull(fileInfoRes)) {
            throw new BaseException(BaseEnum.FILE_INFO_IS_NULL);
        }
        QueryWrapper<FileInfo> queryWrapper = new QueryWrapper<>();
        return fileInfoMapper.selectPage(page, queryWrapper);
    }

    /**
     * 更新附件接口实现
     * @param fileInfoRes
     * @param files
     * @return FileInfoRes
     */
    @Transient
    @Override
    @ApiLogDetail(detail = "核心服务-更新附件接口实现",operationType=BaseEnum.UPDATE)
    public FileInfoRes update(FileInfoRes fileInfoRes, MultipartFile[] files) {
        String filePath = "";
        try {
            // 多文件上传
            for (MultipartFile file : files) {
                // 上传简单文件名
                String originalFilename = file.getOriginalFilename();
                // 存储路径
                filePath = new StringBuilder(fileRootPath)
                        .append(System.currentTimeMillis())
                        .append(originalFilename)
                        .toString();

                // 保存文件
                file.transferTo(new File(filePath));
                FileInfo fileInfo = new FileInfo();
                BeanUtils.copyProperties(fileInfoRes, fileInfo);
                int result = fileInfoMapper.updateById(fileInfo);
                if (result > 0) {
                    FileInfoVo fileInfoVo = new FileInfoVo();
                    BeanUtils.copyProperties(fileInfo, fileInfoVo);
                    return fileInfoRes;
                }
            }
        } catch (IOException e) {
            throw new BaseException(BaseEnum.FILE_UPLOAD_IS_ERROR, e);
        }
        return null;
    }
}
